package com.gewu.teday.activity

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.view.View
import android.widget.*
import androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
import com.gewu.teday.R
import com.gewu.teday.adapter.MoneyAdapter
import com.gewu.teday.adapter.ViewpagerBean
import com.gewu.teday.bean.DataBeans
import com.gewu.teday.fragmnet.TeDayFragment
import com.gewu.teday.fragmnet.ZhengTiFragment
import com.gewu.teday.utils.*
import kotlinx.android.synthetic.main.activity_moeny.*
import kotlinx.android.synthetic.main.toolbar_layout.*

class MoenyActivity : BaseActivity() {

    override fun getLayout(): Int = R.layout.activity_moeny

    val list: ArrayList<ViewpagerBean> = ArrayList()

    override fun initView() {
        toolbar_content.visibility = View.VISIBLE
        toolbar_main.visibility = View.GONE
        tixian_list.visibility = View.GONE
        toolbar_money.title = ""
        viewpager.setNoScroll(false)
        toolbar_money.setBackgroundColor(resources.getColor(R.color.beijing))
        toolbar_tabs.setBackgroundColor(resources.getColor(R.color.beijing))
        setSupportActionBar(toolbar_money)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar_money.setNavigationOnClickListener(View.OnClickListener { finish() })
        list.add(ViewpagerBean(ZhengTiFragment(), "整体收益"))
        list.add(ViewpagerBean(TeDayFragment(), "创作收益"))
        viewpager.adapter = MoneyAdapter(supportFragmentManager, list, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
        toolbar_tabs.setupWithViewPager(viewpager)
        viewpager.currentItem = 0
        update.setOnLongClickListener {
            showDialog()
            true
        }

    }

    var listView = ArrayList<View>()
    var type: Int = 0

    fun showDialog() {
        val build = GwDialog(this).LayoutBuilder()
        val create =
            build.create(R.layout.moeny_dilog_layout, false, Gravity.CENTER, R.drawable.shape)
        create.show()
        val viewlayout = build.getView()
        val cancel = viewlayout?.findViewById<ImageView>(R.id.cancel_im)
        val sucess = viewlayout?.findViewById<TextView>(R.id.sucess)
        val videoNum = viewlayout?.findViewById<EditText>(R.id.video_num)
        val money = viewlayout?.findViewById<EditText>(R.id.money)
        val max_money = viewlayout?.findViewById<EditText>(R.id.max_money)
        val min_money = viewlayout?.findViewById<EditText>(R.id.min_money)
        val leijiMoney = viewlayout?.findViewById<EditText>(R.id.leiji_money)
        val week_radio = viewlayout?.findViewById<TextView>(R.id.week_radio)
        val moeth_radio = viewlayout?.findViewById<TextView>(R.id.moeth_radio)
        val three_radio = viewlayout?.findViewById<TextView>(R.id.three_radio)

        money?.addTextChangedListener(MoneyTextWatcher(money))
        leijiMoney?.addTextChangedListener(MoneyTextWatcher(leijiMoney))

        val decodeParcelable = SpUtils.instances?.decodeParcelable("data", DataBeans::class.java)
        if (decodeParcelable == null) {
            if (videoNum != null) {
                videoNum.hint = "N个视频"
            }
            if (money != null) {
                money.hint = "单位元"
            }
            if (leijiMoney != null) {
                leijiMoney.hint = "单位元"
            }
        } else {
            if (videoNum != null) {
                videoNum.hint = decodeParcelable.videoNum.ifBlank { "0" }
            }
            if (money != null) {
                money.hint = String.format("%.2f", decodeParcelable.money.toFloat()).ifBlank { "0" }
            }
            if (leijiMoney != null) {
                leijiMoney.hint = String.format("%.2f", decodeParcelable.leijiNum.toFloat()).ifBlank { "0" }
            }
            if (max_money != null) {
                max_money.hint = decodeParcelable.maxMoney.ifBlank { "0" }
            }
            if (min_money != null) {
                min_money.hint = decodeParcelable.minMoney.ifBlank { "0" }
            }
        }

        week_radio?.let { listView.add(it) }
        moeth_radio?.let { listView.add(it) }
        three_radio?.let { listView.add(it) }
        week_radio?.let { setViewBac(it) }

        week_radio?.setOnClickListener {
            setViewBac(week_radio)
            type = 0
        }

        moeth_radio?.setOnClickListener {
            setViewBac(moeth_radio)
            type = 1
        }

        three_radio?.setOnClickListener {
            setViewBac(three_radio)
            type = 2
        }

        cancel?.setOnClickListener {
            create.dismiss()
        }

        sucess?.setOnClickListener {
            if (money != null&&max_money!=null&&min_money!=null) {
                if (!money.text.isNullOrEmpty() && !max_money.text.isNullOrEmpty() && !min_money.text.isNullOrEmpty()) {
                    if (money.text.toString().toFloat() < 1800 || money.text.toString().toFloat() > 21000
                    ) {
                        Toast.makeText(this, "金额在(1800<输入<21000)", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }

                    if (max_money.text.toString().toInt() < 0 || min_money.text.toString().toInt() > money.text.toString().toFloat()
                    ) {
                        Toast.makeText(this, "金额在(0<输入<提现金额)", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }

                    if (money.text.toString().toFloat() /30 >= max_money.text.toString().toInt()) {
                        Toast.makeText(this, "最大值输入必须 > 输入除30", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }

                    val splitV = TestRandom.splitV(
                        money.text.toString().toFloat(),
                        31,
                        min_money.text.toString().toInt(),
                    )
                    val mothlist = Util.getMothlist()

                    if (splitV.isEmpty() || mothlist.isEmpty()) {
                        Toast.makeText(this, "异常(最小值太大)", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }

                    if (splitV.size < 31 || mothlist.size < 31) {
                        Toast.makeText(this, "异常", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }

                    //上面的判断 一定要保证日期和金额  生成 并且不能小于31（因为是一个月 30也行）
                    val dataBeans = DataBeans(
                        if (videoNum?.text.isNullOrEmpty()) {
                            decodeParcelable?.videoNum ?: "0"
                        } else {
                            videoNum?.text.toString()
                        },

                        if (money.text.isNullOrEmpty()) {
                            decodeParcelable?.money ?: "0"
                        } else {
                            money.text.toString()
                        },

                        type,

                        if (leijiMoney?.text.isNullOrEmpty()) {
                            decodeParcelable?.leijiNum ?: "0"
                        } else {
                            leijiMoney?.text.toString()
                        },

                        splitV,
                        mothlist,
                        max_money.text.toString(),
                        min_money.text.toString()
                    )

                    handler.postDelayed({
                        val zhengTiFragment = list[0].fragment as ZhengTiFragment
                        val teDayFragment = list[1].fragment as TeDayFragment
                        zhengTiFragment.data(dataBeans)
                        teDayFragment.data(dataBeans)
                    }, 1000)

                    SpUtils.instances?.encode("indexday", 0)
                    SpUtils.instances?.encodeParcelable("data", dataBeans)
                    create.dismiss()
                } else {
                    Toast.makeText(this, "没填写数据", Toast.LENGTH_LONG).show()
                }
            }
        }
    }

    private fun setViewBac(view: TextView) {
        for (i in 0 until listView.size) {
            setColor(5f, "#9CA4A5A6", listView[i])
        }
        setColor(5f, "#FF5722", view)
    }

    fun setColor(radio: Float, color: String, view: View) {
        val drawable = GradientDrawable()
        drawable.cornerRadius = radio
        drawable.setColor(Color.parseColor(color))
        drawable.setStroke(0, -0x1000000)
        view.background = drawable
    }

    override fun initData() {
    }

}